package com.igeek.dao;

import com.igeek.domain.Customer;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

/**
 * @author jerry
 * @Company 极客营
 */
@Repository
public class CustomerDao {
    @Autowired
    private DataSource dataSource;
    private QueryRunner qr = null;
    //Customer{cust_id='null', cust_name='tom11', cust_user_id='null',
    // cust_create_id='null', cust_source='2', cust_industry='null',
    // cust_level='1', cust_linkman='3', cust_phone='0108888887', cust_mobile='13888888888'}

    private String sql = "";
    public void save(Customer c) {

        try {
            qr = new QueryRunner(dataSource);
            sql = "insert into cst_customer (cust_id,cust_name,cust_source,cust_level," +
                    "cust_linkman,cust_phone,cust_mobile) values (null,?,?,?,?,?,?)";
            qr.update(sql,c.getCust_name(),c.getCust_source(),c.getCust_level(),
                    c.getCust_linkman(),c.getCust_phone(),c.getCust_mobile());
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public List<Customer> findAll() {
        try {
            qr = new QueryRunner(dataSource);
            sql = "select * from cst_customer";
            return  qr.query(sql ,new BeanListHandler<>(Customer.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void deleteById(String cust_id) {
        try {
            sql = "delete from cst_customer where cust_id = ?";
            qr = new QueryRunner(dataSource);
            qr.update(sql,cust_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Customer findById(String cust_id) {
        try {
            qr = new QueryRunner(dataSource);
            sql = "select * from cst_customer where cust_id = ?";
            return qr.query(sql,new BeanHandler<>(Customer.class),cust_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //Customer{cust_id='null', cust_name='tom11', cust_user_id='null',
    // cust_create_id='null', cust_source='2', cust_industry='null',
    // cust_level='1', cust_linkman='3', cust_phone='0108888887', cust_mobile='13888888888'}

    public void update(Customer c) {
        try {
            qr = new QueryRunner(dataSource);
            sql = "update cst_customer set cust_name = ?,cust_source=?,cust_level=?" +
                    ",cust_linkman=?,cust_phone=?,cust_mobile=?" +
                    " where cust_id=?";
            qr.update(sql,c.getCust_name(),c.getCust_source(),c.getCust_level(),
                    c.getCust_linkman(),c.getCust_phone(),c.getCust_mobile(),
                    c.getCust_id());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<Customer> queryByCondition(String cust_name) {
        try {
            qr = new QueryRunner(dataSource);
            sql = "select * from cst_customer where cust_name like  '%"+cust_name+"%'";
            return  qr.query(sql,new BeanListHandler<>(Customer.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
